<?php

namespace App\Http\Service;

use App\Http\Util\Result;
use App\Model\FunctionDetail;
use App\Model\Scenery;
use Illuminate\Support\Facades\DB;

trait ServiceFunctionDetail{

    /**
     * czs
     * @return mixed
     * 景区规则分类列表
     */
    public static function getList($id,$limit){
        $list = FunctionDetail::where("function_id",$id)->orderBy('id','desc')->paginate($limit);
        return Result::success()->setData($list);
    }

    /**
     * @return mixed
     * 未勾选的景点
     * czs
     */
    public static function leaveCheck(){
        $res = Scenery::where('detail_id',0)->orwhere('detail_id',NULL)->get();
        return Result::success()->setData($res);

    }

    /**
     *未生成规则
     * czs
     */
    public static function leaveDetail($functionId) {
        $detail1 = FunctionDetail::where('function_id',$functionId)->where('type',1)->count();
        $detail2 = FunctionDetail::where('function_id',$functionId)->where('type',2)->count();
        if (!empty($detail1)&&!empty($detail2)) {
            return Result::success()->setData([3,4,5]);
        }
        if (!empty($detail1)&&empty($detail2)) {
            return Result::success()->setData([2,3,4,5]);
        }
        if (empty($detail1)&&!empty($detail2)) {
            return Result::success()->setData([1,3,4,5]);
        }
        return Result::success()->setData([1,2,3,4,5]);
    }

    /**
     * @return mixed
     * 已勾选的景点
     * czs
     */
    public static function editCheck($id){
        $res = Scenery::where('detail_id',$id)->get();
        return Result::success()->setData($res);

    }

    /**
     * 保存景点
     * czs
     */
    public static function saveData($para) {
        $detailId = $para['detail_id'];
        unset($para["detail_id"]);
        DB::beginTransaction();
        try {
        $id = FunctionDetail::insertGetId($para);
        foreach ($detailId as $v) {
            Scenery::where('id',$v)->update(['detail_id' => $id]);
        }
            DB::commit();
        } catch (Exception $e){
            DB::rollback();
            throw $e;
        }
        return Result::success()->setData("保存成功！");
    }

    /**
     * 更新景点
     * czs
     */
    public static function updateData($para,$id) {
        if ($para['start_time']>=$para['end_time']) {
            return Result::failure()->setMsg("开始时间勿大于结束时间！");
        }
        if (!empty($para['detail_id'])) {
            $detailId = $para['detail_id'];
            unset($para["detail_id"]);
        }
        DB::beginTransaction();
        try {
            FunctionDetail::where('id',$id)->update($para);
            Scenery::where('detail_id',$id)->update(['detail_id' => 0]);
            if (!empty($detailId)) {
                foreach ($detailId as $v) {
                    Scenery::where('id',$v)->update(['detail_id' => $id]);
                }
            }
            DB::commit();
        } catch (Exception $e){
            DB::rollback();
            throw $e;
        }
        return Result::success()->setData("更新成功！");
    }

    /**
     *获取单个id记录
     * czs
     */
    public static function getOne($id) {
        $res = FunctionDetail::where("id", $id)->first();
        return Result::success()->setData($res);
    }

    /**
     * czs
     *删除记录
     */
    public static function deleteLine($id) {
        DB::beginTransaction();
        try {
            $res = FunctionDetail::where("id", $id)->delete();
            Scenery::where('detail_id',$id)->update(['detail_id' => 0]);
            DB::commit();
        } catch (Exception $e){
            DB::rollback();
            throw $e;
        }
        return Result::success()->setData($res);
    }
}


